//
//  WedDreamCollectionViewCell.m
//  MMLOOK
//
//  Created by dllo on 15/9/28.
//  Copyright © 2015年 武俊娥. All rights reserved.
//

#import "WedDreamCollectionViewCell.h"

#import "WedPullCollectionViewCell.h"
#import "MyLayout.h"
#import "WedModel.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"



@interface WedDreamCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain)UICollectionView *collection;

@property(nonatomic, retain)MyLayout *flow;

@property(nonatomic, retain)MBProgressHUD *mbProView;



@end

@implementation WedDreamCollectionViewCell


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
      
        self.pageCount = 1;

       self.flow = [[MyLayout alloc]init];
        
        self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:self.flow];
        
        self.collection.backgroundColor = [UIColor whiteColor];
        
        [self.collection registerClass:[WedPullCollectionViewCell class] forCellWithReuseIdentifier:@"= ="];
        
        self.collection.delegate = self;
        self.collection.dataSource = self;
        
        [self addSubview:self.collection];
        [_collection release];
        
        
        //下拉刷新
        [self refresh];
        
        //上拉加载
        [self refreshFoot];
        
        
    }
    return self;
}


//下拉刷新
-(void)refresh
{
    
    self.collection.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
         self.pageCount = 1;
        
        //数组第几个url 进行网络请求
        [self.delegate beginRefresh:self];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.collection.header endRefreshing];
        
    }];
    
    
}


//上拉加载
-(void)refreshFoot
{
    
    self.collection.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        
        NSLog(@"=============%ld", self.pageCount);
        
        self.pageCount = self.pageCount + 1;
        
        //数组第几个url 进行网络请求
        [self.delegate beginRefresh:self];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.collection.footer endRefreshing];
        
    }];
    
    
}


#pragma mark -
#pragma mark 获得数据

-(void)setCollectArray:(NSMutableArray *)collectArray
{
    
    if (_collectArray != collectArray) {
        
        [_collectArray release];
        
        _collectArray = [collectArray retain];
    }
    
    //如果数组为0 返回到上一页去进行网络请求
    if (self.collectArray.count == 0) {

        if (self.flag == NO) {
            
            //先读取本地
            [self.delegate readLocal:self];
            
            //数组第几个url 进行网络请求
//            [self.delegate beginRefresh:self];
            
        }
        
        
    } else {
        
    self.flow.modelArr  = [NSMutableArray arrayWithArray:collectArray];
    
    //刷新界面
    [self.collection reloadData];
        
    }
    
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.collectArray.count;
    
}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    WedPullCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"= =" forIndexPath:indexPath];

    [cell sendNameId:[self.collectArray objectAtIndex:indexPath.item]];
    
    return cell;
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    WedModel *model = [self.collectArray objectAtIndex:indexPath.item];
 
    self.sendIdBlock(model);
    
}



-(void)pushDetailWithSendIdBlock:(void(^)(WedModel *model))block
{
    self.sendIdBlock = block;
    
}


@end
